import { TODO_ADDTASK, TODO_DELBYID, TODO_UPDATEBYID } from '../action/actionType';

const initialState = {
  list: [
    {
      id: 1,
      task: 'React',
      isDone: false,
    },
    {
      id: 2,
      task: 'React2',
      isDone: true,
    },
    {
      id: 3,
      task: 'React3',
      isDone: false,
    },
  ],
  type: 'all',
};

export default function todoReducer(state = initialState, { type, payload }) {
  console.warn('Logger:type, payload  ----->  ', type, payload);
  switch (type) {
    case TODO_UPDATEBYID:
      return {
        ...state,
        list: state.list.map((item) => ({
          ...item,
          isDone: item.id === payload ? !item.isDone : item.isDone,
        })),
      };
    case TODO_DELBYID:
      return {
        ...state,
        list: state.list.filter((item) => item.id !== payload),
      };

    case TODO_ADDTASK:
      return {
        ...state,
        list: [payload, ...state.list],
      };

    default:
      return state;
  }
}
